﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ProjectVLIB_LAB04.KetNoiDB;
using System.Data.SqlClient;
using ProjectVLIB_LAB04.VOL;

namespace ProjectVLIB_LAB04.BLL
{
    class DMHangBL
    {
        private DataAccess dataAccess;

        public DMHangBL()
        {
            dataAccess = new DataAccess();
        }

        /// <summary>
        /// 1. Hiển thị danh sách hang
        /// </summary>
        /// <returns></returns>
        public DataTable GetData()
        {
            // cau query select
            string sql = "SELECT MaHang, TenHang, HinhAnh, DVT, DonGia, MaNhaCungCap, MaLoaiHang, MoTa FROM tbl_DMHang ORDER BY MaHang DESC ";

            DataTable dt = new DataTable();
            // do toan bo du lieu vao datatable
            dt = dataAccess.GetData(sql);
            return dt;
        }

        /// <summary>
        /// 2. Thêm hang
        /// </summary>
        /// <param name="obj">DMHang</param>
        /// <returns>true</returns>
        public bool InsertData(DMHang obj)
        {
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@TenHang", SqlDbType.NVarChar) { Value = obj.TenHang };
            param[1] = new SqlParameter("@HinhAnh", SqlDbType.NVarChar) { Value = obj.HinhAnh };
            param[2] = new SqlParameter("@DVT", SqlDbType.NVarChar) { Value = obj.DVT };
            param[3] = new SqlParameter("@DonGia", SqlDbType.Float) { Value = obj.DonGia };
            param[4] = new SqlParameter("@MaNhaCungCap", SqlDbType.Int) { Value = obj.MaNhaCungCap };
            param[5] = new SqlParameter("@MaLoaiHang", SqlDbType.Int) { Value = obj.MaLoaiHang };
            param[6] = new SqlParameter("@MoTa", SqlDbType.NVarChar) { Value = obj.MoTa };

            // cau query select
            string sql = "INSERT INTO tbl_DMHang (TenHang, HinhAnh, DVT, DonGia, MaNhaCungCap, MaLoaiHang, MoTa) VALUES (@TenHang, @HinhAnh, @DVT,@DonGia,@MaNhaCungCap,@MaLoaiHang,@MoTa)";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 3. Xóa hang
        /// </summary>
        /// <param name="MaHang">MaHang INT</param>
        /// <returns>true</returns>
        public bool DeleteData(int MaHang)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHang", SqlDbType.Int) { Value = MaHang };
            // cau query select
            string sql = "DELETE FROM tbl_DMHang WHERE MaHang = @MaHang";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 4. sửa hang
        /// </summary>
        /// <param name="obj">DMHang</param>
        /// <returns>true</returns>
        public bool UpdateData(DMHang obj)
        {
            SqlParameter[] param = new SqlParameter[8];
            param[0] = new SqlParameter("@TenHang", SqlDbType.NVarChar) { Value = obj.TenHang };
            param[1] = new SqlParameter("@HinhAnh", SqlDbType.NVarChar) { Value = obj.HinhAnh };
            param[2] = new SqlParameter("@DVT", SqlDbType.NVarChar) { Value = obj.DVT };
            param[3] = new SqlParameter("@DonGia", SqlDbType.Float) { Value = obj.DonGia };
            param[4] = new SqlParameter("@MaNhaCungCap", SqlDbType.Int) { Value = obj.MaNhaCungCap };
            param[5] = new SqlParameter("@MaLoaiHang", SqlDbType.Int) { Value = obj.MaLoaiHang };
            param[6] = new SqlParameter("@MoTa", SqlDbType.NVarChar) { Value = obj.MoTa };
            param[7] = new SqlParameter("@MaHang", SqlDbType.Int) { Value = obj.MaHang };

            // cau query select
            string sql = "UPDATE tbl_DMHang SET TenHang = @TenHang, HinhAnh = @HinhAnh, DVT = @DVT, DonGia = @DonGia, MaNhaCungCap = @MaNhaCungCap, MaLoaiHang = @MaLoaiHang, MoTa = @MoTa  " +
                           " WHERE MaHang = @MaHang";
            dataAccess.Execute(sql, param);

            return true;
        }

        /// <summary>
        /// 5. Tìm theo ma hang
        /// </summary>
        /// <param name="MaHang"></param>
        /// <returns></returns>
        public DataTable GetDataById(int MaHang)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("@MaHang", SqlDbType.Int) { Value = MaHang };
            // cau query select
            string sql = "SELECT * FROM tbl_DMHang WHERE MaHang = @MaHang";
            DataTable dt = dataAccess.GetData(sql, param);

            return dt;
        }

        // 6. Login
        // 7. Logout
    }
}
